package fetch

import (
	"bufio"
	"golang.org/x/net/html/charset"
	"golang.org/x/text/encoding"
	"golang.org/x/text/encoding/unicode"
	"golang.org/x/text/transform"
	"io/ioutil"
	"net/http"
)

func Fetch(url string)([]byte, error){
	var content []byte
	resp, err:=http.Get("https://www.zhenai.com/zhenghun")
	if err!=nil{
		panic(err)
	}
	defer resp.Body.Close()
	if resp.StatusCode!=http.StatusOK{
		return content ,err
	}
	newReader:=bufio.NewReader(resp.Body)
	e:=determineEncode(newReader)
	utd8Reader:=transform.NewReader(newReader,e.NewDecoder())
	content, err =ioutil.ReadAll(utd8Reader)
	return content,err
}

func determineEncode(r *bufio.Reader)encoding.Encoding{
	bytes,err:=r.Peek(1024)
	if err!= nil {
		return unicode.UTF8
	}
	e,_,_:=charset.DetermineEncoding(bytes,"")
	return e
}